package cn.hs.ph.servlet;

import cn.hs.ph.dao.impl.CommunityDaoImpl;
import cn.hs.ph.entity.Community;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码方式
        //获取action
        String action = req.getParameter("action");
        //根据action判断
        if ("selectAll".equals(action)){
            //读取信息
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String communityName = req.getParameter("communityName");
            //封装到map集合
            Map<String,String> map = new HashMap<>();
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("communityName",communityName);
            //连接数据库
            List<Community> list = new CommunityDaoImpl().selectAll(map);
            int total = new CommunityDaoImpl().selectCount(map);
            //响应
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result result = Result.success(resultMap);

            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("update".equals(action)){
            //获取信息
            String addOrUpdateFlag = req.getParameter("addOrUpdateFlag");
            String communityAddress = req.getParameter("communityAddress");
            String code = req.getParameter("communityAreaCode");
            int communityAreaCode = 0;
            if (code!=null&&!"".equals(code)){
                communityAreaCode = Integer.parseInt(code);
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");
            String communityDistrict = req.getParameter("communityDistrict");
            String id = req.getParameter("communityId");
            int communityId = 0;
            if (id!=null&&!"".equals(id)){
                communityId = Integer.parseInt(id);
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");

            Community community = new Community(
                    communityId,
                    communityName,
                    communityProvince,
                    communityCity,
                    communityDistrict,
                    communityAddress,
                    communityLatitude,
                    communityLongitude,
                    communityAreaCode,
                    communityCoordinates
            );
            //判断是新增还是修改
            if ("1".equals(addOrUpdateFlag)){
                int num = new CommunityDaoImpl().insert(community);
                Result result = null;
                if (num!=0){
                    result = Result.success("添加成功！");
                }else {
                    result = Result.error("添加失败！");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }else {
                int num = new CommunityDaoImpl().update(community);
                Result result = null;
                if (num!=0){
                    result = Result.success("修改成功！");
                }else {
                    result = Result.error("修改失败！");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }else if ("delete".equals(action)){
            String id = req.getParameter("communityId");

            int communityId = Integer.parseInt(id);

            int num = new CommunityDaoImpl().delete(communityId);
            Result result = null;
            if (num!=0){
                result = Result.success("删除成功！");
            }else {
                result = Result.error("删除失败！");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
